package com.example.zhangxuemin.fragmenttest130zxm.model;

import com.example.zhangxuemin.fragmenttest130zxm.bean.UserBean;
import com.example.zhangxuemin.fragmenttest130zxm.iface.LoginListener;
import com.example.zhangxuemin.fragmenttest130zxm.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhangxuemin on 2017/5/31.
 */

public class LoginModel {
    private Retrofit retrofit;
    private static String BASE_URL="http://amicool.neusoft.edu.cn/";
    public LoginModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public void login(String username,String password,final LoginListener listener){
        LoginService loginService=retrofit.create(LoginService.class);
        Call<UserBean> call=loginService.login(username,password);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("用户不存在");
            }


            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
